<?php
/**
 * 配置管理类
 * 
 * @author Yucy<yucy1985@gmail.com>
 *
 */
class YucyConfig
{
	
	private static $_config = array();
	
	
	/**
	 * 
	 * 返回所有配置信息
	 */
	public static function getAll()
	{
		return self::$_config;
	}
	
	
	/**
	 *
	 * 获取配置
	 * @param string $name 配置名，支持a.b.c
	 * @return mixed
	 */
	public static function get($name)
	{
		if (!$name) return null;
		
		$arr_name = self::_normalizeConfigName($name);
		$first_name = array_shift($arr_name);
		if (!isset(self::$_config[$first_name]) || empty(self::$_config[$first_name])) return null;
		
		$value = self::$_config[$first_name];
		
		foreach ($arr_name as $val)
		{
			if ($value == null) break;
			$value = isset($value[$val]) ? $value[$val] : null;
		}
		return $value;
	}
	
	
	/**
	 * 
	 * 检查配置项是否存在
	 * @param string $name
	 * @return bool
	 */
	public static function has($name)
	{
		if (!$name) return false;
		
		$arr_name = self::_normalizeConfigName($name);
		$first_name = array_shift($arr_name);
		if (!isset(self::$_config[$first_name])) return false;
		
		$count = count($arr_name);
		if (!$count) return true;
		
		$value = self::$_config[$first_name];
		$flag = false;
		foreach ($arr_name as $val)
		{
			$flag = isset($value[$val]) ? true : false;
			if ($flag === false) break;
			$value = $value[$val];
		}
		
		return $flag ? true : false;
	}
	
	
	/**
	 * 
	 * 生成多维数组
	 * @param array $key
	 * @param mixed $value
	 */
	private static function _setValue($key, $value = '')
	{
		if (count($key) < 1) return $value;
		
		$_config = array();
		
		$_key = array_shift($key);
		$_config[(string)$_key] = self::_setValue($key, $value);
		return $_config;
	}
	
	
	/**
	 *
	 * 设置配置参数
	 * @param string $name
	 * @param mixed $value
	 */
	public static function set($name, $value)
	{
		if (empty($name)) return false;
		
		$arr_name = self::_normalizeConfigName($name);
		$count_name = count($arr_name);
		if ($count_name == 1)
		{
			self::$_config[(string)$arr_name['0']] = $value;
			return true;
		}
		
		// 如果存在则覆盖掉
		if (self::has($name))
		{
			self::$_config = array_merge(self::$_config, self::_setValue($arr_name, $value));
			return true;
		}
		
		self::$_config = array_merge_recursive(self::$_config, self::_setValue($arr_name, $value));
		return true;
	}
	
	
	/**
	 *
	 * 分析配置名
	 * @param string $name
	 * @return array
	 */
	private static function _normalizeConfigName($name)
	{
		$arr_name = explode('.', $name);
		return (array)$arr_name;
	}
	
	
	/**
	 *
	 * 移除配置
	 * @param string $name
	 */
	public static function delete($name)
	{
		if (isset(self::$_config[$name])) unset(self::$_config[$name]);
	}



	/**
	 *
	 * 清空配置
	 */
	public static function clear()
	{
		self::$_config = array();
	}
	
	
	/**
	 * 
	 * 导入配置信息 会覆盖掉之前存在的
	 * @param array $config
	 */
	public static function import(array $config)
	{
		if (empty($config)) return ;
		self::$_config = array_merge(self::$_config, $config);
	}
	
	
	/**
	 *
	 * 加载配置文件
	 * @param string|array $file
	 */
	public static function loadFile($file)
	{
		!is_array($file) && $file = array($file);
		
		foreach ($file as $_file)
		{
			if (!file_exists($_file)) continue;
			$config = include_once($_file);
			if (!is_array($config)) continue;
			self::import($config);
		}
	}
}
?>